//
//  NSDate+sinaDate.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/25.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit

extension NSDate {
    
    class func sinaCreateDate(sinaStr sinaStr: String?) -> NSDate? {
        
        guard let timeStr = sinaStr else {
            return nil
        }
        
        DateFormator.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        DateFormator.locale = NSLocale(localeIdentifier: "en_US")
        
        //获取原来的日期
        let resDate = DateFormator.dateFromString(timeStr)!
        return resDate
    }
    
    var dateDescription: String {
        
        var createText:String = ""
        
        let curLendar = NSCalendar.currentCalendar()
        //是今年发的
        if isThisYear(self) {
            //今天
            if curLendar.isDateInToday(self) {
                
                let timeinterval = abs(Int32(self.timeIntervalSinceNow))
                if timeinterval < 60 {
                    createText = "刚刚"
                } else if timeinterval < 3600 {
                    createText = "\(timeinterval / 60)分钟之前"
                } else {
                    createText = "\(timeinterval / 3600)小时之前"
                }
            } else if curLendar.isDateInYesterday(self) {
                //昨天
                DateFormator.dateFormat = "HH:mm"
                createText = "昨天: \(DateFormator.stringFromDate(self))"
            } else {
                //其他
                DateFormator.dateFormat = "MM-dd HH:mm"
                createText = DateFormator.stringFromDate(self)
            }
        } else {
            //不是今年发的
            DateFormator.dateFormat = "yyyy-MM-dd HH:mm"
            createText = DateFormator.stringFromDate(self)
        }
        return createText
    }
    
//    class func getCreateText(date date: NSDate?) -> String {
//        
//        guard let dd = date else {
//            return ""
//        }
//        
//        var createText: String = ""
//        //获得当前日期
//        let curLendar = NSCalendar.currentCalendar()
//        //是今年发的
//        if isThisYear(dd) {
//            //今天
//            if curLendar.isDateInToday(dd) {
//                
//                let timeinterval = abs(Int32(dd.timeIntervalSinceNow))
//                if timeinterval < 60 {
//                    createText = "刚刚"
//                } else if timeinterval < 3600 {
//                    createText = "\(timeinterval / 60)分钟之前"
//                } else {
//                    createText = "\(timeinterval / 3600)小时之前"
//                }
//            } else if curLendar.isDateInYesterday(dd) {
//                //昨天
//                DateFormator.dateFormat = "HH:mm"
//                createText = "昨天: \(DateFormator.stringFromDate(dd))"
//            } else {
//                //其他
//                DateFormator.dateFormat = "MM-dd HH:mm"
//                createText = DateFormator.stringFromDate(dd)
//            }
//        } else {
//            //不是今年发的
//            DateFormator.dateFormat = "yyyy-MM-dd HH:mm"
//            createText = DateFormator.stringFromDate(dd)
//        }
//        return createText
//    }
    func isThisYear(date:NSDate) -> Bool {
        
        DateFormator.dateFormat = "yyyy"
        let createStr = DateFormator.stringFromDate(date)
        let curStr = DateFormator.stringFromDate(NSDate())
        return createStr == curStr
    }

}
